#!/bin/bash

cd `dirname $0`

# 使用mvn命令很慢
#jar_file=$(mvn -q \
#    -Dexec.executable=echo \
#    -Dexec.args='${project.artifactId}-${project.version}.jar' \
#    --non-recursive \
#    exec:exec)
artifactId=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='artifactId']/text()" pom.xml)
version=$(xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml)
jar_file=$artifactId-$version.jar


if [ "$1" == "1" ]; then
    mvn clean package -U -Dmaven.test.skip=true
fi

# pkill 可能干掉自己这个进程
pname="config-server"
pid=`jps | awk -v name="$pname" '$0 ~ name {print $1}'`
if [ -n "$pid" ]; then
    kill -9 $pid
    sleep 1
fi

truncate -s0 nohup.out
nohup \
java \
    -Djava.security.egd=file:/dev/./urandom \
    -Duser.timezone=Asia/Shanghai \
    -jar "$PWD/target/$jar_file" \
        --spring.profiles.active=prod \
&

tail -f nohup.out


# 如何动态的使用端口
# https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application
# springboot 通过 server.port 来指定端口

# 1. application argument
# java -jar </path/to/jar> --server.port=9999

# 2. SPRING_APPLICATION_JSON spring boot 1.3.0 +
# 2.1 环境变量
# SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar </path/to/jar>

# 2.2 by using Java System property
# java -Dspring.application.json='{"server.port":7788}' java -jar </path/to/jar>

# 2.3
# java -jar <jar> --spring.application.json='{"server.port":7788}'

# jvm system property
# java -Dserver.port=9999 -jar <jar>

# env
# SERVER_PORT=9999 java -jar <jar>